9. BCS
Business Connectivity Services (BCS)
makes it easy to integrate SharePoint with external systems. To do so,
BCS maps external systems capabilities to standardized interfaces to
define how to interact with them. At the core, there are four concepts
in BCS that you must understand:
- External system — Any supported
source of data that BCS can access using out-of-the-box or custom
connectors. Examples of external systems are a web service, a database, a
Line of Business (LOB) system, Web 2.0 service, or even a .NET object
that contains data.
- External content type (ECT) — An
external content type defines the schema and data access capabilities of
an external system and its behavior. External content types are often
referred to as the building blocks of BCS.
NOTE Conceptually,
you can think of an external content type as a business entity. An
entity is an abstraction of something real such as a customer, sales
order, and so on. An entity is an entity whether used in BCS, C# class,
Entity Framework, or database schema. Each entity has a name, meta data,
associations, and stereotyped operations — such as create, read,
update, and delete — that enables access to external data. Each entity
can have multiple instances such as rows in a database table, SharePoint
list items, or instances of an object.
- BDC model (BDCM) — The XML
representation of one or more external content types along with
resources, such as localized strings, meta data, permissions, and
connectivity information. With a BDC model, developers do not need to
learn the nuances of each external system.
- External list — External lists
enable users to present LOB data as if they are native SharePoint lists.
In external lists the data does not actually reside within SharePoint;
instead it is pulled directly from the backend external system each time
the list is accessed.
Thankfully, BCS core concepts have not changed in
SharePoint 2013. However, there are a few enhancements. Now review the
changes.
OData Connector
SharePoint 2013 ships with an OData
connector for BCS. This enables developers to consume data from
OData-compliant services in their BCS solutions. In the Windows Azure
marketplace, Microsoft exposes many free or paid OData data sources that
you can consume in your BCS solutions. For example, you can consume the
free Northwind OData source at http://services.odata.org/Northwind/Northwind.svc/. The following code snippet shows a portion of a BDCM built around the Northwind OData source:
<LobSystem Name="ODataNWModel" Type="OData">
<Properties>
<Property Name="ODataServiceMetadataUrl" Type="System.String">
http://services.odata.org/Northwind/Northwind.svc/$metadata</Property>
<Property Name="ODataMetadataAuthenticationMode"
Type="System.String">PassThrough</Property>
<Property Name="ODataServicesVersion" Type="System.String">2.0</Property>
</Properties>
<LobSystemInstances>
<LobSystemInstance Name="http://services.odata.org/Northwind/Northwind.svc">
<Properties>
<Property Name="ODataServiceUrl" Type="System.String">
http://services.odata.org/Northwind/Northwind.svc
</Property>
<Property Name="ODataServiceAuthenticationMode"
Type="System.String">PassThrough</Property>
<Property Name="ODataFormat"
Type="System.String">application/atom+xml</Property>
</Properties>
</LobSystemInstance>
</LobSystemInstances>
<!-- Code Omitted for brevity -->
</LobSystem>
As discussed earlier, SharePoint list data is
already exposed as an OData source. When you put everything together,
it’s fair to say SharePoint can be a producer and a consumer of OData
sources.
BCS Powered Apps
One of the issues with ECTs in
SharePoint 2010 was that they had to be configured at the farm level and
required farm administrators to be involved. With the new programming
model introduced in SharePoint 2013 and the emphasis on cloud computing
and granular isolation, this is not possible anymore.
In SharePoint 2013, developers can include ECTs in
their apps and access external data from any external system such as
Netflix or Windows Azure directly from their apps without involving the
tenant administrator.
NOTE Only
Visual Studio 2012 has the support to generate BDCM from an OData
source. There is currently no support for this in SharePoint Designer
2013.
The following code snippet demonstrates how an external list can be defined in an app:
<ListInstance Url="$Resources:core,lists_Folder;/BCSEmpoweredList"
Description=" A BCS Empowered External List"
OnQuickLaunch="TRUE" Title="BCS-ECT" Id="BCSEmpoweredList">
<DataSource>
<Property Value="" Name="LobSystemInstance"/>
<Property Value="" Name="EntityNamespace"/>
<Property Value="" Name="Entity"/>
<Property Value="" Name="SpecificFinder"/>
<Property Name="MetadataCatalogFileName"
Value="BDCMetadata.bdcm" />
</DataSource>
</ListInstance>
As you can see the code snippet has a few elements, each representing a property for the external list. For example, the Entity property should contain the name of the business entity that this external list points to in the backend system.
CSOM and REST Interface
Just like other workloads discussed so far, BCS has a new JavaScript object model to support remote calls.
For example, the following REST query returns all
customers from an external list called Customers. This list is deployed
by an app:
http://server/sites/sc/_api/lists/getbytitle('Customers')/items
The following code snippet shows how to make the
same REST call, but uses JavaScript against CSOM and then binds the
results to a grid:
BCSEmpoweredList.Grid.prototype = {
init: function () {
$.ajax({
url: this.surlWeb + "_api/lists/getbytitle('Customers')/
items?$select=BdcIdentity,CustomerID,ContactName",
headers: {
"accept": "application/json",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: this.showItems
});
}
The following code snippet shows how to use a BCS object model in C# code against CSOM:
var ctx = new SP.ClientContext();
var web = ctx.get_web();
entity = web.getAppBdcCatalog().getEntity(entityNameSpace, entityName);
ctx.load(entity);
lobSystem = entity.getLobSystem();
ctx.load(lobSystem);
lobSystemInstances = lobSystem.getLobSystemInstances();
ctx.load(lobSystemInstances);
ctx.executeQueryAsync(success, failure);
The code starts with establishing the context and
referencing the business entity. Once the entity is referenced, the
content is loaded and two event handlers are registered for call backs.
In the success event handler, you will write code to deal with the
returned result.